package org.jeecg.modules.vinQuery.util;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;


import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.Scanner;

public class Test1 {

    private static WebDriver drivers;

    public static boolean isAlertPersent(){
        try {
            drivers.switchTo().alert();
            return true;
        }catch (NoAlertPresentException e){return false;}
    }

    public static String getAlert(){
        Alert alert = drivers.switchTo().alert();
        String text = alert.getText();
        alert.accept();
        return text;
    }

    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.edge.driver", "D:/EdgeDriver/msedgedriver.exe");
//        System.setProperty("webdriver.chrome.driver", "D:/ChromeDriver/chromedriver.exe");
        try {

            // 创建WebDriver
            drivers = new EdgeDriver();

            drivers.get("https://info.vecc.org.cn/ve/vin/index");
//            driver.manage().window().maximize();//窗口最大化
            drivers.findElement(By.name("vin")).sendKeys("LRDS6PEB5MT508484");
            drivers.findElement(By.name("fdjh")).sendKeys("985836");

            saveImg(drivers);
            System.out.println("----------------------------------------------------------------");
            String name;
            do {
                System.out.println("请输入验证码");
                Scanner scanner = new Scanner(System.in);
                name=scanner.next();
                if ("刷新".equals(name)) {
                    drivers.findElement(By.id("vaild")).click();
                    Thread.sleep(100);
                    saveImg(drivers);
                } else {
                    drivers.findElement(By.name("vaild")).sendKeys(name);
                    drivers.findElement(By.cssSelector("img[src='../images/query.jpg']")).click();
                    if (isAlertPersent()){
                        String alert = getAlert();
                        System.out.println(alert);
                        name = "刷新";
                    }
                }
            } while ("刷新".equals(name));
            String currentUrl = drivers.getCurrentUrl();
            System.out.println("当前页面的URL:" + currentUrl); //输出当前的url链接
            // 获取DevTools对象
//            DevTools devTools = ((HasDevTools) drivers).getDevTools();
//            devTools.createSession();

            // 监听所有网络请求
//            devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//            drivers.get(currentUrl);
//            devTools.addListener(Network.responseReceived(), res -> {
//                System.out.println(res.getResponse().getUrl());
//            });

            Document doc = Jsoup.connect(currentUrl).get();
            System.out.println(doc);
            Elements elements = doc.getElementsByClass("MsoNormal");
            System.out.println(elements.size());
            for (int i = 0; i < elements.size(); i++) {
                Element element = elements.get(i);
                String content = element.text();
                if ("1 车辆型号：".equals(content)) {
                    System.out.println("车辆型号：" + elements.get(i+1).text());
                }
                if ("2 商 标：".equals(content)) {
                    System.out.println("车辆品牌：" + elements.get(i+1).text());
                }
                if ("4 排放阶段：".equals(content)) {
                    System.out.println("排放阶段：" + elements.get(i+1).text());
                }
                if ("8 发动机型号：".equals(content)) {
                    System.out.println("发动机型号：" + elements.get(i+1).text());
                }
                if ("10 制造商名称：".equals(content)) {
                    System.out.println("制造商名称：" + elements.get(i+1).text());
                }
            }
            System.out.println("----------------------------------------------------------------");
            drivers.quit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void saveImg(WebDriver driver) throws IOException {
        WebElement vaild = driver.findElement(By.id("vaild"));
        //定义图片存放路径包括文件名
        String eleLocalFileFullName = "D:\\temp\\imgTest\\Test.png";
        //本地文件转为文件类型
        File eleLocalFile = new File(eleLocalFileFullName);
        //截取二维码图片并下载到本地默认目录
        File eleScreenShotFile = vaild.getScreenshotAs(OutputType.FILE);
        //判断指定目录中Test.png是否存在如果存在则删除
        if(eleLocalFile.exists()){
            eleLocalFile.delete();
        }
        //将本地默认路径中的验证码截图文件移动到指定目标
        FileUtils.moveFile(eleScreenShotFile, eleLocalFile);
    }

}
